package fule.com.playandroidkotlin.ui.fragment

import android.app.ActivityOptions
import android.content.Intent
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.youth.banner.Banner
import com.youth.banner.BannerConfig
import com.youth.banner.Transformer
import com.youth.banner.listener.OnBannerListener
import fule.com.playandroidkotlin.base.BaseFragment
import fule.com.playandroidkotlin.R
import fule.com.playandroidkotlin.base.Model
import fule.com.playandroidkotlin.ui.activity.WebViewActivity
import fule.com.playandroidkotlin.ui.adapter.ArticleListAdapter
import fule.com.playandroidkotlin.ui.model.ArticleListModel
import fule.com.playandroidkotlin.ui.model.BannerModel
import fule.com.playandroidkotlin.ui.presenter.BannerPresenter
import fule.com.playandroidkotlin.ui.util.AppConstant
import fule.com.playandroidkotlin.ui.util.GlideImageLoader
import fule.com.playandroidkotlin.ui.view.BannerView
import kotlinx.android.synthetic.main.fm_home.*
import kotlinx.android.synthetic.main.toolbar.*
import java.util.*



/**
 * 作者： njb
 * 时间： 2018/8/7 0007-下午 5:50
 * 描述： 首页
 * 来源：
 */
@Suppress("DEPRECATION")
class HomeFragment : BaseFragment<BannerPresenter>(), BannerView {
    override fun setTitle() {
        toolbar_title!!.text="首页"
    }


    private var articleAdapter: ArticleListAdapter? = null
    private var mList: List<ArticleListModel.DatasBean>? = null
    private var viewHead: View? = null
    private var bannerModel: List<BannerModel>? = null
    private var banner: Banner? = null
    private var page = 0

    override fun getLayoutId(): Int {
        return R.layout.fm_home
    }

    override fun loadData() {
        presenter!!.banner()
        presenter!!.articleList(page)
    }

    override fun initView() {
        viewHead = LayoutInflater.from(context).inflate(R.layout.item_home_banner, null)
        initBanner()
        initAdapter()
    }

    private fun initBanner() {
        banner = viewHead!!.findViewById(R.id.home_banner)
        //设置banner样式
        banner!!.setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
        //设置图片加载器
        banner!!.setImageLoader(GlideImageLoader())
        //设置banner动画效果
        banner!!.setBannerAnimation(Transformer.Default)
        //设置自动轮播，默认为true
        banner!!.isAutoPlay(true)
        //设置轮播时间
        banner!!.setDelayTime(3000)
        //设置指示器位置（当banner模式中有指示器时）
        banner!!.setIndicatorGravity(BannerConfig.CENTER)
        //banner设置方法全部调用完毕时最后调用
        banner!!.start()

        banner!!.setOnBannerListener(OnBannerListener { position ->
            if (bannerModel == null) {
                return@OnBannerListener
            }
            Intent(context, WebViewActivity::class.java).run {
                putExtra(AppConstant.WEBVIEW_URL_KEY, bannerModel!![position].url)
                putExtra(AppConstant.WEBVIEW_TITLE_KEY, bannerModel!![position].title)
                putExtra(AppConstant.WEBVIEW_ID_KEY, bannerModel!![position].id)
                startActivity(this)
            }
        })
    }

    private fun initAdapter() {
        articleAdapter = ArticleListAdapter(null)
        articleAdapter.run {
            val manager = LinearLayoutManager(context)
            rv_article.layoutManager = manager
            articleAdapter!!.addHeaderView(viewHead)
    /*        this!!.bindToRecyclerView(rv_article)
            this.setEmptyView(R.layout.empty_layout_no_data)*/
        }
        rv_article.run {
            rv_article.adapter = articleAdapter
        }

        articleAdapter!!.setOnItemClickListener { _, View, position ->
            val options: ActivityOptions = ActivityOptions.makeScaleUpAnimation(View,
                    View.width / 2,
                    View.height / 2,
                    0,
                    0)
            Intent(context, WebViewActivity::class.java).run {
                putExtra(AppConstant.WEBVIEW_URL_KEY, articleAdapter!!.data[position].link)
                putExtra(AppConstant.WEBVIEW_TITLE_KEY, articleAdapter!!.data[position].title)
                putExtra(AppConstant.WEBVIEW_ID_KEY, articleAdapter!!.data[position].id)
                startActivity(this,options.toBundle())
            }
        }

    }

    override fun addListener() {
        rf_article.setOnRefreshListener {
            page = 0
            presenter!!.articleList(page)
        }
        rf_article.setOnLoadMoreListener {
            page++
            presenter!!.articleList(page)
        }
    }

    override fun createPresenter(): BannerPresenter {
        return BannerPresenter(this)
    }

    override fun showLoading() {
    }

    override fun onErrorCode(model: Model) {
        super.onErrorCode(model)
        rf_article.finishLoadMore(300)
        rf_article.finishRefresh(300)
    }

    override fun onLoadBanner(data: List<BannerModel>) {
        val list = ArrayList<String>()
        for (m in data) {
            list.add(m.imagePath!!)
        }
        bannerModel = data
        banner!!.update(list)
    }

    override fun onLoadArticleList(data: ArticleListModel) {
        mList = data.datas
        if (page == 0) {
            articleAdapter!!.setNewData(data.datas)
        } else {
            articleAdapter!!.addData(data.datas!!)
        }
        rf_article.finishRefresh(300)
        rf_article.finishLoadMore(300)
    }

}
